﻿using Newtonsoft.Json;
using System.Web.Mvc;
using SilenthinkContract;
using SilenthinkContract.Domain;
using Newtonsoft.Json.Converters;
using SilenthinkCommon.Extension;
using SilenthinkContract.Component;

namespace Web.Areas.Auction.Controllers
{
    public class BidController : Controller
    {
        private static readonly JsonConverter Converter = new JavaScriptDateTimeConverter();
        //private static readonly ServiceClientProxy<IBidService> BidProxy = new ServiceClientProxy<IBidService>("BidService");
        public ActionResult Index(long param = 0)
        {
            var result = new SkResult<object>(false);
            var isAuthenticated = Request.IsAuthenticated;
            if (!isAuthenticated)
            {
                result.Message = "请先登录";
                var data = JsonConvert.SerializeObject(result, Converter);
                return Content(data, "application/json");
            }
            if (param < 1)
            {
                result.Message = "参数不正确";
                var data = JsonConvert.SerializeObject(result, Converter);
                return Content(data, "application/json");
            }
            var auctionId = param;
            var userId = User.Identity.SkIdentity().Id;
            var ipAddress = Request.UserHostAddress;
            var s = new BidRequester().Bid(userId, auctionId, ipAddress);
            //var data = JsonConvert.SerializeObject(s, Converter);
            return Content(s, "application/json");
        }

        public bool Open()
        {
            return new BidRequester().Open();
        }
        public bool Close()
        {
            return new BidRequester().Close();
        }
    }
}
